/*
 * This file is part of LangMan.
 *
 * LangMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * LangMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LangMan.  If not, see <http://www.gnu.org/licenses/>.
 */

package langman.util.homogenizer;

import langman.Utils;
import langman.business.config.Config;
import langman.business.config.ConfigService;
import langman.business.config.LanguageDescription;
import langman.business.config.SetDescription;
import langman.business.data.TranslationException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.util.List;


public class Homogenizer {
    private static final Logger logger = LogManager.getLogger(Homogenizer.class);

    public static void main(String... args) {
        if (args.length != 2) {
            logger.warn("Usage: Homogenizer <root path> <config file>");
            System.exit(-1);
        }

        File rootPath = new File(args[0]);
        File configFile = new File(args[1]);

        Homogenizer homogenizer = new Homogenizer();
        try {
            ConfigService configService = new ConfigService();
            Config config = configService.loadConfig(configFile);
            homogenizer.homogenize(rootPath, config);
        } catch (TranslationException e) {
            logger.error(e.getMessage(), e);
        }
    }

    public void homogenize(File rootPath, Config config) throws TranslationException {
        List<SetDescription> setDescriptions = config.getSetDescriptions();
        for (SetDescription setDescription : setDescriptions) {
            LanguageDescription reference = determineReferenceLanguage(setDescription);

            if (reference == null) {
                throw new TranslationException("There is no reference language defined in config!");
            }

            File referenceFile = new File(rootPath, reference.getFileName());
            try (ReferenceLoader referenceLoader = new ReferenceLoader(referenceFile)) {
                ReferenceDescription referenceDescription = referenceLoader.loadLanguage();
                writeLanguages(rootPath, setDescription, referenceDescription);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    private void writeLanguages(File rootPath, SetDescription setDescription, ReferenceDescription referenceDescription) throws TranslationException {
        for (LanguageDescription languageDescription : setDescription.getLanguageDescriptions()) {

            File sourceFile = new File(rootPath, languageDescription.getFileName());
            File tempFile = new File(rootPath, languageDescription.getFileName() + "_TEMP");

            try (ReferenceBasedHomogenizer languageWriter = new ReferenceBasedHomogenizer(referenceDescription, sourceFile, tempFile)) {
                languageWriter.writeLanguage();
            } catch (Exception e) {
                throw new TranslationException(e);
            }

            try {
                Utils.overwriteWithTempFile(tempFile, sourceFile);
            } catch (IOException e) {
                throw new TranslationException(e);
            }
        }
    }

    private LanguageDescription determineReferenceLanguage(SetDescription setDescription) {
        LanguageDescription reference = null;
        List<LanguageDescription> languageDescriptions = setDescription.getLanguageDescriptions();
        for (LanguageDescription languageDescription : languageDescriptions) {
            if (languageDescription.isReference()) {
                reference = languageDescription;
                break;
            }
        }
        return reference;
    }


}
